package com.mrduan.otgfilemanager.ui_sdcard.main.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.github.mjdev.libaums.fs.UsbFile;
import com.mrduan.otgfilemanager.Config;
import com.mrduan.otgfilemanager.R;
import com.mrduan.otgfilemanager.common.ShareActivity;
import com.mrduan.otgfilemanager.ui_sdcard.video.VideoPlayActivity;
import com.mrduan.otgfilemanager.utils.CalendarUtil;
import com.mrduan.otgfilemanager.utils.CustomRotateAnim;
import com.mrduan.otgfilemanager.utils.Lunar;
import com.mrduan.otgfilemanager.utils.MediaFileUtil;
import com.mrduan.otgfilemanager.utils.SuanFa;
import com.orhanobut.logger.Logger;
import com.shike.baselibrary.fragment.BaseFragment;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by snoopy on 2018/5/2.
 */

public class TwoScreenFragment extends BaseFragment implements View.OnClickListener {

    private TextView text1, text2, text3, text4, text5, text6;
    private List<String> mFilePath = new ArrayList<>();
    private ImageView imgView;
    private TextView tishi_text;

    private ImageView two_img_1,two_img_2,two_img_3,two_img_4,two_img_5;

    private SuanFa suanFa;

    @Override
    public void initGetDataFromParent(Bundle savedInstanceState) {

    }

    @Override
    public int initSetContentView() {
        return R.layout.two_screen_fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        text6.setText(suanFa.getYuan());

        if (!suanFa.isToday()) {
            startAnimation();
        }
    }

    @Override
    public void initBindWidget(Bundle savedInstanceState) {

        Calendar calendar=Calendar.getInstance();
        Lunar lunar = new Lunar(calendar);

        TextView yinli = getView(R.id.yinli);
        yinli.setText(lunar.toString());//农历的月日

        TextView yangli = getView(R.id.yangli);
        yangli.setText(CalendarUtil.getStrTime(calendar) + " " + CalendarUtil.getWeekDay(calendar));

        text1 = getView(R.id.two_text_1);
        text2 = getView(R.id.two_text_2);
        text3 = getView(R.id.two_text_3);
        text4 = getView(R.id.two_text_4);
        text5 = getView(R.id.two_text_5);
        text6 = getView(R.id.two_text_6);

        two_img_1 = getView(R.id.two_img_1);
        two_img_2 = getView(R.id.two_img_2);
        two_img_3 = getView(R.id.two_img_3);
        two_img_4 = getView(R.id.two_img_4);
        two_img_5 = getView(R.id.two_img_5);

        imgView = getView(R.id.img_view);
        tishi_text = getView(R.id.tishi_text);
    }

    @Override
    public void initSetListener() {
        getView(R.id.two_btn_1).setOnClickListener(this);
        getView(R.id.two_btn_2).setOnClickListener(this);
        getView(R.id.two_btn_3).setOnClickListener(this);
        getView(R.id.two_btn_4).setOnClickListener(this);
        getView(R.id.two_btn_5).setOnClickListener(this);
        getView(R.id.two_btn_6).setOnClickListener(this);
    }

    @Override
    public void initSetData() {
        List<String> imgPath = new ArrayList<>();

        suanFa = new SuanFa(getContext());

        if (!suanFa.isToday()) {
            startAnimation();
        }

        List<String> fileName = new ArrayList<>();

        for (String arg : Config.twoScreenFiles) {
            File file = new File(Config.BASE_FILE_PATH + "/" + arg);
            File[] files = file.listFiles();

            for (File f : files) {
                if (f.isDirectory()) {
                    fileName.add(f.getName());
                    mFilePath.add(f.getPath());

                    File[] getImgFiles = f.listFiles();
                    for (File getImg : getImgFiles) {
                        if (MediaFileUtil.isImageFileType(getImg.getPath())) {
                            imgPath.add(getImg.getPath());
                            break;
                        }
                    }

                    break;
                }
            }
        }

        if (fileName.size() >= 5) {
            text1.setText(fileName.get(0));
            text2.setText(fileName.get(1));
            text3.setText(fileName.get(2));
            text4.setText(fileName.get(3));
            text5.setText(fileName.get(4));
            text6.setText(suanFa.getYuan());

            Glide.with(getContext())
                    .load(imgPath.get(0))
                    .into(two_img_1);
            Glide.with(getContext())
                    .load(imgPath.get(1))
                    .into(two_img_2);
            Glide.with(getContext())
                    .load(imgPath.get(2))
                    .into(two_img_3);
            Glide.with(getContext())
                    .load(imgPath.get(3))
                    .into(two_img_4);
            Glide.with(getContext())
                    .load(imgPath.get(4))
                    .into(two_img_5);
        }
    }











    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.two_btn_6) {

            if (suanFa.isToday()) { // 不是动态图
                startActivity(new Intent(getContext(), ShareActivity.class));
                return;
            }

            suanFa.saveYuan();

            // 组装分享内容
            String sText = "";
            String sTitle = "";
            String url = "";
            List<String> imgPathList = new ArrayList<>();

            File[] files = Config.shareFile.listFiles();
            for (File arg : files) {
                if (arg.getName().indexOf("share") > -1) { // 分享内容
                    sTitle = arg.getName().replace(".share","");
                } else if (arg.getName().indexOf("stitle") > -1) {
                    sText = arg.getName().replace(".stitle","");
                } else if (arg.getName().indexOf("surl") > -1){
                    url = arg.getName().replace(".surl","");
                } else {
                    imgPathList.add(arg.getPath());
                }
            }


            OnekeyShare oks = new OnekeyShare();
            //关闭sso授权
            oks.disableSSOWhenAuthorize();

            // title标题，微信、QQ和QQ空间等平台使用
            oks.setTitle(sTitle);
            // text是分享文本，所有平台都需要这个字段
            oks.setText(sText);
            if (imgPathList.size() > 0) {
                String[] imgs = new String[imgPathList.size()];
                for (int i = 0;i<imgPathList.size();i++) {
                    imgs[i] = imgPathList.get(i);
                }
                oks.setImageArray(imgs);
//                oks.setImagePath(imgs[0]);
            }
            oks.setCallback(new PlatformActionListener() {
                @Override
                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                    showToast("分享成功");
                    suanFa.saveYuan();
                    text6.setText(suanFa.getYuan());
                    imgView.clearAnimation();
                    tishi_text.setVisibility(View.GONE);
                    Logger.d("成功");
                }

                @Override
                public void onError(Platform platform, int i, Throwable throwable) {
                    showToast("分享失败");
                    Logger.d("失败");
                }

                @Override
                public void onCancel(Platform platform, int i) {
                    showToast("分享取消");
                    Logger.d("取消");
                }
            });
            if (url.length() > 0) {
                // url在微信、微博，Facebook等平台中使用
                oks.setUrl(url);
            }
            oks.setPlatform(WechatMoments.NAME);
            // 启动分享GUI
            oks.show(getContext());


            return;
        }

        String path = "";
        switch (view.getId()) {
            case R.id.two_btn_1:
                path = mFilePath.get(0);
                break;
            case R.id.two_btn_2:
                path = mFilePath.get(1);
                break;
            case R.id.two_btn_3:
                path = mFilePath.get(2);
                break;
            case R.id.two_btn_4:
                path = mFilePath.get(3);
                break;
            case R.id.two_btn_5:
                path = mFilePath.get(4);
                break;
        }

        Intent intent = new Intent(getContext(), VideoPlayActivity.class);
        intent.putExtra("path",path);
        startActivity(intent);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    int needShow = 3;
    /**
     * 动画
     */
    private void startAnimation(){
        CustomRotateAnim rotateAnim = CustomRotateAnim.getCustomRotateAnim();
        // 一次动画执行1秒
        rotateAnim.setDuration(1000);
        // 设置为循环播放
        rotateAnim.setRepeatCount(-1);
        // 设置为匀速
        rotateAnim.setInterpolator(new AccelerateInterpolator());

        rotateAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                if (--needShow == 0) {
                    needShow = 3;
                    if (tishi_text.getVisibility() == View.VISIBLE) {
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tishi_text.setVisibility(View.GONE);
                            }
                        });
                    } else {
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tishi_text.setVisibility(View.VISIBLE);
                            }
                        });
                    }
                }
            }
        });
        imgView.startAnimation(rotateAnim);//开始动画
    }

}
